//中文转成unicode
function cn2unicode(str) {
	var value = '';
	for (var i = 0; i < str.length; i++) {
		value += '#z' + left_zero_4(parseInt(str.charCodeAt(i)).toString(16));
	}
	return value;
}

//unicode补齐四位
function left_zero_4(str) {
	if (str != null && str != '' && str != 'undefined') {
		if (str.length == 2) {
			return '00' + str;
		}
	}
	return str;
}

//unicode转成中文
function unicode2cn(str) {
	str = str.replace(/(#z)(\w{1,5})/gi,function($0) {
		var v0 = escape($0).replace(/(%23z)(\w{1,5})/g,"$2");
		var v1 = v0.substr(0,2)+v0.substr(3,2);
		return (String.fromCharCode(parseInt(v1,16)));
		// return (String.fromCharCode(parseInt((escape($0).replace(/(%23z)(\w{1,5})/g,"$2")),16)));
	});
	str = str.replace(/(%x)(\w{1,5})/gi,function($0) {
		var v0 = escape($0).replace(/(%25x)(\w{1,5})/g,"$2");
		var v1 = v0.substr(0,1)+v0.substr(2,3);
		return (String.fromCharCode(parseInt(v1,16)));
	});
	// str = str.replace(/(&#x)(\w{1,4});/gi,function($0) {
	// 	return String.fromCharCode(parseInt(escape($0).replace(/(%26%23x)(\w{1,4})(%3B)/g,"$2"),16));
	// });
	// str = str.replace(/(&#)(\d{1,6});/gi,function($0) {
	// 	return String.fromCharCode(parseInt(escape($0).replace(/(%26%23)(\d{1,6})(%3B)/g,"$2")));
	// });
	return str;
}

//unicode转成中文(js)
function unicode2cn2(str) {
	str = str.replace(/(#z)(\w{1,5})/gi,function($0) {
		var v0 = escape($0).replace(/(%23z)(\w{1,5})/g,"$2");
		var v1 = v0.substr(0,2)+v0.substr(3,2);
		return (String.fromCharCode(parseInt(v1,16)));
	});
	str = str.replace(/(%x)(\w{1,5})/gi,function($0) {
		var v0 = escape($0).replace(/(%25x)(\w{1,5})/g,"$2");
		var v1 = v0.substr(0,1)+v0.substr(2,3);
		return (String.fromCharCode(parseInt(v1,16)));
	});
	return str;
}

//检查转换指定元素
function chk_unicode(is_run,add_tags,default_tags) {
	var v1 = 'title,a,div,span,p,b,font,td,button,h4,option,label,input';
	var v2 = default_tags ? default_tags.split(',') : v1.split(',');
	var v3 = add_tags ? add_tags.split(',') : new Array();
	for (var i=v3.length-1; i>=0; i--) {
		v2.push(v3[i]);
	}
	for (var i=v2.length-1; i>=0; i--) {
		set_unicode(v2[i]);
	}
	setTimeout(function() {
		var keys = _getElementsByClassName('unicode_key');
		if (is_run && keys.length > 0) {
			for (var i=0; i<keys.length; i++) {
				if (keys[i].tagName == 'INPUT') {
					keys[i].value = keys[i].nextSibling.value;
				} else {
					keys[i].innerText = keys[i].nextSibling.value;
				}
			}
		}
	},5);
}

//转换指定元素
function set_unicode(tag) {
	var v1 = document.getElementsByTagName(tag);
	var v2 = /(#z)(\w{1,5})/;
	var v3 = /(%x)(\w{1,5})/;
	for (var i=v1.length-1; i>=0; i--) {
		if (v1[i].title && (v2.test(v1[i].title) || v3.test(v1[i].title))) v1[i].title = unicode2cn(v1[i].title);
		if (v1[i].value && (v2.test(v1[i].value) || v3.test(v1[i].title))) v1[i].value = unicode2cn(v1[i].value);
	}
	try {
		for (var i=v1.length-1; i>=0; i--) {
			var vv = v1[i].getElementsByTagName('*');
			if (vv.length < 1 && v1[i].innerText && (v2.test(v1[i].innerText) || v3.test(v1[i].innerText))) {
				v1[i].innerText = unicode2cn(v1[i].innerText);
			} else if (v1[i].innerHTML && (v2.test(v1[i].innerHTML) || v3.test(v1[i].innerHTML))) {
				v1[i].innerHTML = unicode2cn(v1[i].innerHTML);
			}
		}
	} catch (e) {}
}

function _getElementsByClassName(className,tagName) { 
	tagName = tagName||"*";
	if (document.getElementsByClassName) {
		return document.getElementsByClassName(className);
	} else { 
		var tag = document.getElementsByTagName(tagName);
		var tagAll = [];
		for (var i = 0; i<tag.length; i++) {
			for (var j=0,n=tag[i].className.split(' '); j<n.length; j++) {
				if (n[j] == className) {
					tagAll.push(tag[i]);
					break;
				}
			}
		}
		return tagAll;
	}
}